package de.gameduell.net.src.connection {

	import de.gameduell.net.src.communication.ICommunication;
	import de.gameduell.net.src.connection.LoadVarsConnection;
	import de.gameduell.net.src.message.IMessage;
	import de.gameduell.net.src.protocol.CommunicationRequests;	

	public class PollConnection extends LoadVarsConnection {
		
		public static const CONNECTION_TYPE:String = "poll";
		
		private var synchronisation:Synchronisation;

		public function PollConnection(aCommunication:ICommunication,syncTime:int) {
			super(aCommunication);
			synchronisation = Synchronisation.getInstance();
			synchronisation.init(communication, syncTime);
		}

		override public function connect(host:String, port:int):void {
			super.connect(host, port);
			synchronisation.sync();
		}
		
		override public function close():void {
			super.close();
			synchronisation.close();
		}

		override public function sendMessage(message:IMessage):void {
			if(message.requestType == CommunicationRequests.SYNC) {
				message = synchronisation.updateSyncMessage(message);
			}
			super.sendMessage(message);
		}
		
		override public function getType():String {
			return CONNECTION_TYPE;
		}
	}
}
